﻿#include "dispatchvideo.h"
#include <QLayout>
#include <QPainter>
#include <QPaintEvent>
#include <QToolButton>
#include "channelinfo.h"
#include "characterset.h"
#include "clientcallback.h"

DispatchVideo::DispatchVideo(QWidget *parent) : VideoWindow(parent), bottomBar(new QWidget(this)), isSpeaking(false)
{
    bottomBar->hide();

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));

    QSize iconSize(32, 32);

    auto* button = new QToolButton();
    button->setIcon(QIcon(":/gui/phone_left90.png"));
    button->setIconSize(iconSize);
    connect(button, &QAbstractButton::clicked, this, &DispatchVideo::turnLeft);
    layout->addWidget(button);

    button = new QToolButton();
    button->setIcon(QIcon(":/gui/phone_right90.png"));
    button->setIconSize(iconSize);
    connect(button, &QAbstractButton::clicked, this, &DispatchVideo::turnRight);
    layout->addWidget(button);

    button = new QToolButton();
    button->setIcon(QIcon(":/gui/phone_camera.png"));
    button->setIconSize(iconSize);
    connect(button, &QAbstractButton::clicked, this, &DispatchVideo::switchCamera);
    layout->addWidget(button);
/*
    button = new QToolButton();
    button->setText(QObject::trECSUTF8("对讲"));
    layout->addWidget(button);
*/
    button = new QToolButton();
    button->setCheckable(true);
    QIcon icon;
    icon.addPixmap(QPixmap(":/gui/phone_silent.png"), QIcon::Normal, QIcon::Off);
    icon.addPixmap(QPixmap(":/gui/phone_voice.png"), QIcon::Normal, QIcon::On);
    button->setIcon(icon);
    button->setIconSize(iconSize);
    connect(button, &QAbstractButton::toggled, this, &DispatchVideo::toggleSpeak);
    layout->addWidget(button);

    button = new QToolButton();
    button->setIcon(QIcon(":/gui/video_close.png"));
    button->setIconSize(iconSize);
    connect(button, &QAbstractButton::clicked, this, &DispatchVideo::closeVideo);
    layout->addWidget(button);

    layout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));

    layout->setMargin(0);
    layout->setSpacing(0);
    bottomBar->setLayout(layout);
    bottomBar->setStyleSheet(".QToolButton{ border: 1px none #90C8F6; background-color: rgba(0, 0, 0, 75%); } \
                             .QToolButton:hover{ border-style: solid }");
}

DispatchVideo::~DispatchVideo()
{
    toggleSpeak(false);
}

void DispatchVideo::paintEvent(QPaintEvent *e)
{
    VideoWindow::paintEvent(e);
    QPainter painter(this);
    if(!bottomRect.isNull() && e->rect().intersects(bottomRect))
    {
        //画名字
        if(!uname.isEmpty())
            painter.drawText(bottomRect, Qt::AlignCenter, uname);
    }
    //选中背景
    if(isSpeaking)
    {
        auto drawRect = rect();;
        const auto penWidth = 2;
        drawRect.adjust(penWidth - 1, penWidth - 1, 1-penWidth, 1-penWidth);
        if(!drawRect.contains(e->rect()))
        {
            QPainter painter(this);
            auto pen = painter.pen();
            pen.setColor(Qt::red);
            pen.setWidth(penWidth);
            painter.setPen(pen);
            painter.drawRect(drawRect);
        }
    }
}

void DispatchVideo::resizeEvent(QResizeEvent *e)
{
    VideoWindow::resizeEvent(e);
    if(!videoRect.isNull()) {
        bottomBar->setGeometry(videoRect.left(), videoRect.bottom() - bottomBar->sizeHint().height(), videoRect.width(), bottomBar->sizeHint().height());
    }
}

void DispatchVideo::setToolBarVisible(bool visible)
{
    VideoWindow::setToolBarVisible(visible);
    bottomBar->setVisible(visible);
}

void DispatchVideo::turnLeft()
{
    ClientCallback::instance()->im_send_msg(uid, QString(), msg_type_dispatch_turn_left, QString());
}

void DispatchVideo::turnRight()
{
    ClientCallback::instance()->im_send_msg(uid, QString(), msg_type_dispatch_turn_right, QString());
}

void DispatchVideo::switchCamera()
{
    ClientCallback::instance()->im_send_msg(uid, QString(), msg_type_dispatch_switch_camera, QString());
}

void DispatchVideo::toggleSpeak(bool checked)
{
    if(isSpeaking == checked)
        return;

    if(checked)
    {
        ClientCallback::instance()->im_send_msg(uid, QString(), msg_type_notify, P2PChanneInfo::start_talk);
        ClientCallback::instance()->create_p2p_channel(uid);
    }
    else
    {
        ClientCallback::instance()->im_send_msg(uid, QString(), msg_type_notify, P2PChanneInfo::end_talk);
        ClientCallback::instance()->free_p2p_channel();
    }

    isSpeaking = checked;
    update();
}

void DispatchVideo::closeVideo()
{
    emit close_video(uid);
}

void DispatchVideo::setSpeaking(bool speaking)
{
    if(isSpeaking == speaking)
        return;
    isSpeaking = speaking;
    update();
}
